package luj.tool.jnproxyan.proxy.network.socks.cmd.connect2;

import io.netty.buffer.ByteBuf;
import luj.bean.api.bean.Bean;
import luj.net.api.server.FrameDataReceiver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class P2sReceive implements FrameDataReceiver {

  P2sReceive(ReqInfo req) {
    _req = req;
  }

  @Override
  public Result receive(Context ctx) throws Exception {
    ByteBuf frameBuf = ctx.getLastFrame();
    forwardToClient(frameBuf);

    return ctx.then()
        .nextReceiver(this);
  }

  private void forwardToClient(ByteBuf frameBuf) {
    if (frameBuf == null) {
      return;
    }

    byte[] data = new byte[frameBuf.readableBytes()];
    frameBuf.readBytes(data);

    Long reqId = _req._reqId;
    LOG.debug("网站发<-：#{}，大小：{}", reqId, data.length);

//    Bean<Server2CForwardMsg> msg = _req._lujbean.createBean(Server2CForwardMsg.class);
//    msg.setField(m -> m::reqId, reqId);
//    msg.setField(m -> m::data, data);
//    _req._socksRef.tell(msg.getValueInstance());
  }

  private static final Logger LOG = LoggerFactory.getLogger(P2sReceive.class);

  private final ReqInfo _req;
}
